package com.jie.jie;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.util.List;

/**
 * Created by Administrator on 2017/1/19.
 */

public class MainRvAdapter extends RecyclerView.Adapter<MainRvAdapter.MyViewholder> {
    private List<String> datas;
    private Context context;
    private Itemonclick itemonclick;

    public void setItemonclick(Itemonclick itemonclick) {
        this.itemonclick = itemonclick;
    }

    public MainRvAdapter(List<String> datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public MyViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.main_rv_item, parent, false);

        return new MyViewholder(view);
    }

    @Override
    public void onBindViewHolder(MyViewholder holder, final int position) {
        holder.button.setText(datas.get(position));
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemonclick.onclick(v,position);
            }
        });

    }

    interface Itemonclick {
        void onclick(View view, int position);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    static class  MyViewholder extends RecyclerView.ViewHolder {
        Button button;

        public MyViewholder(View itemView) {
            super(itemView);
            this.button = (Button) itemView.findViewById(R.id.main_item_main);
        }
    }
}
